# Maintainer: Some One <some.one@some.email.com>

_realname=z3
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.6.0
pkgrel=1
pkgdesc="Z3 is a high-performance theorem prover being developed at Microsoft Research (mingw-w64)"
arch=('any')
url="https://github.com/Z3Prover/z3"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "python3")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/Z3Prover/z3/archive/z3-${pkgver}.tar.gz"
        mingw-fixes.patch)
sha256sums=('511da31d1f985cf0c79b2de05bda4e057371ba519769d1546ff71e1304fe53c9'
            '5ad322bbaff39ebae94d5092f495c89e35ff6b53da88a86a2c1c6d972ac66b5b')

apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${_realname}-${pkgver}
  apply_patch_with_msg mingw-fixes.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH}-shared ]] && rm -rf "${srcdir}"/build-${CARCH}-shared
  cp -rf "${srcdir}"/${_realname}-${_realname}-${pkgver} "${srcdir}"/build-${CARCH}-shared

  cd "${srcdir}"/build-${CARCH}-shared

  CXX=${MINGW_PREFIX}/bin/g++ CC=${MINGW_PREFIX}/bin/gcc \
  AR=${MINGW_PREFIX}/bin/gcc-ar \
  /usr/bin/python3 scripts/mk_make.py \
      --prefix=${MINGW_PREFIX} \
      --staticlib \
      --staticbin

  cd build
  make
}

check() {
   cd "${srcdir}"/build-${CARCH}-shared/build
   make test
}

package() {
   cd "${srcdir}"/build-${CARCH}-shared/build
   make install DESTDIR="${pkgdir}"
   mv ${pkgdir}/${MINGW_PREFIX}/{lib,bin}/libz3.dll
   cp "${srcdir}"/build-${CARCH}-shared/build/libz3.dll.a ${pkgdir}/${MINGW_PREFIX}/lib
}
